const bcrypt = require('bcrypt')
const AsyncMysqljs = require('../mysql/index')
const Query = AsyncMysqljs().query
module.exports = async (ctx, next) => {
    let {username, password} = ctx.request.body
   
    if(!username || !password) {
        ctx.status = 400;
        ctx.body = {
            code: 1,
            data: {},
            msg: '请输入完整的账号密码！',
        }
        return;
    }
    password = await bcrypt.hash(password, 5);
    const users = await Query('SELECT * FROM user WHERE username=?', username);
    
    if(users.length !== 0) {
        ctx.body = {
            code: 0,
            data: {},
            msg: '该用户已经注册过了！',
        }
    } else {
        const user = await Query('insert into user(username,password) values(?, ?)', [username, password])
        ctx.body = {
            code: 1,
            data: {},
            msg: '注册成功',
        }
    }
}